﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static System.Net.Mime.MediaTypeNames;

public class TextureSaver
{
    public static void DeleteUserTexture(long deviceID, int texId)
    {
        DeleteTexture(ServerConfig.textureDir, deviceID, texId);
    }
    public static void DeleteMomentPhoto(long deviceID, int texId)
    {
        DeleteTexture(ServerConfig.momentPhotosDir, deviceID, texId);
    }
    public static int SaveUserTexture(long deviceID, int texId, int texWidth, int texHeight, byte[] tex)
    {
        return SaveTexture(ServerConfig.textureDir, deviceID,texId, texWidth, texHeight, tex);
    }
    public static Tex LoadUserTexture(long deviceID, int texId)
    {
        return LoadTexture(ServerConfig.textureDir, deviceID, texId);
    }
    public static int SaveMomentPhoto(long deviceID,long momentId, int texId, Tex tex)
    {
        return SaveMomentPhoto(deviceID, momentId, texId, tex.texWidth, tex.texHeight, tex.tex);
    }
    public static int SaveMomentPhoto(long deviceID, long momentId, int texId, int texWidth, int texHeight, byte[] tex)
    {
        return SaveTexture(ServerConfig.momentPhotosDir, deviceID, momentId+"_"+texId, texWidth, texHeight, tex);
    }
    public static Tex LoadMomentPhoto(long deviceID, long momentId,int texId)
    {
        return LoadTexture(ServerConfig.momentPhotosDir, deviceID, momentId + "_" +texId);
    }
    public static void DeleteTexture(string texDir, long deviceID, int texId)
    {
        DeleteTexture(texDir, deviceID, texId.ToString());
    }
    public static void DeleteTexture(string texDir, long deviceID, string texId)
    {
        string texFilePath = GetTexFilePath(texDir, deviceID, texId);
        if (File.Exists(texFilePath))
            File.Delete(texFilePath);
        string infoFilePath = GetInfoFilePath(texDir, deviceID, texId);
        if (File.Exists(infoFilePath))
            File.Delete(infoFilePath);
    }
    public static int SaveTexture(string texDir, long deviceID, int texId, int texWidth, int texHeight, byte[] tex)
    {
        return SaveTexture(texDir, deviceID, texId.ToString(), texWidth, texHeight, tex);
    }
    public static int SaveTexture(string texDir, long deviceID, string texId, int texWidth, int texHeight, byte[] tex)
    {
        try
        {
            if (!Directory.Exists(texDir))
            {
                GameDebug.Log($"create texture dir:{texDir}");
                Directory.CreateDirectory(texDir);
            }
            //string texString = Encoding.UTF8.GetString(tex);
            string texFilePath = GetTexFilePath(texDir,deviceID, texId);
            string infoFilePath = GetInfoFilePath(texDir,deviceID, texId);

            string info = GetInfo(texWidth, texHeight);

            File.WriteAllBytes(texFilePath, tex);
            File.WriteAllText(infoFilePath, info);
            return 0;
        }
        catch(Exception ex)
        {
            GameDebug.LogException(ex);
            return -1;
        }
    }
    public static Tex LoadTexture(string texDir, long deviceID, int texId)
    {
        return LoadTexture(texDir, deviceID, texId.ToString());
    }
    public static Tex LoadTexture(string texDir,long deviceID, string texId)
    {
        string texFile = GetTexFilePath(texDir,deviceID, texId);
        if (File.Exists(texFile))
        {
            Tex tex = new Tex();
            tex.tex = File.ReadAllBytes(texFile);

            LoadInfo(texDir,deviceID, texId, out int texWidth, out int texHeight);

            tex.texWidth = texWidth;
            tex.texHeight = texHeight;

            return tex;
        }
        return null;
    }

    public static string GetInfo(int texWidth, int texHeight)
    {
        return texWidth + "_" + texHeight;
    }

    public static void LoadInfo(string texDir, long deviceID, string texId,out int texWidth,out int texHeight)
    {
        texWidth = texHeight = 0;

        string infoFilePath = GetInfoFilePath(texDir,deviceID, texId);
        if (File.Exists(infoFilePath))
        {
            string info = File.ReadAllText(infoFilePath);
            string[] strs = info.Split('_');
            if (strs.Length == 2)
            {
                int.TryParse(strs[0], out texWidth);
                int.TryParse(strs[1], out texHeight);
            }
        }
    }

    public static string GetTexFilePath(string texDir,long deviceID, string texId)
    {
        string fileName = deviceID + "_" + texId + ".jpg";
        string filePath = texDir + "/" + fileName;// ServerConfig.textureDir + "/" + fileName;
        return filePath;
    }
    public static string GetInfoFilePath(string texDir, long deviceID, string texId)
    {
        string fileName = deviceID + "_" + texId + ".txt";
        string filePath = texDir + "/" + fileName; ;// ServerConfig.textureDir + "/" + fileName;
        return filePath;
    }

    static string GetTextureUrl(string texDir, string relativePath,long userID, string texId)
    {
        string fileName = userID + "_" + texId + ".jpg";

        string url = "";
#if !RELEASE
        url = $"file://{GetTexFilePath(texDir, userID, texId)}";
#else
        url = $"https://{ServerConfig.domain}/chapapp/{relativePath}/{fileName}";
#endif

        //GameDebug.Log($"GetTextureUrl:{url}");
        return url;
    }

    public static string GetUserTextureUrl(long userID, int texId, bool nullOnNoTex = true)
    {
        if (nullOnNoTex)
        {
            string texFile = GetTexFilePath(ServerConfig.textureDir, userID, texId.ToString());
            if (!File.Exists(texFile))
            {
                return "";
            }
        }
        return GetTextureUrl(ServerConfig.textureDir, ServerConfig.textureRelativePath, userID, texId.ToString());
    }
    public static string GetMomentPhotoUrl(long userID,long momentId, int texId,bool nullOnNoTex = true)
    {
        if (nullOnNoTex)
        {
            string texFile = GetTexFilePath(ServerConfig.momentPhotosDir, userID, momentId + "_" + texId);
            if (!File.Exists(texFile))
            {
                return "";
            }
        }

        return GetTextureUrl(ServerConfig.momentPhotosDir, ServerConfig.momentPhotosRelativePath, userID, momentId + "_" + texId);
    }
}

